package cn.edu.sgu.www.mhxysy.service.equipment.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.base.Range;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.equipment.EquipmentViewAttributeDTO;
import cn.edu.sgu.www.mhxysy.entity.equipment.EquipmentCategory;
import cn.edu.sgu.www.mhxysy.entity.equipment.EquipmentViewAttribute;
import cn.edu.sgu.www.mhxysy.entity.equipment.ForgeType;
import cn.edu.sgu.www.mhxysy.entity.except.Property;
import cn.edu.sgu.www.mhxysy.mapper.equipment.EquipmentCategoryMapper;
import cn.edu.sgu.www.mhxysy.mapper.equipment.EquipmentTypeMapper;
import cn.edu.sgu.www.mhxysy.mapper.equipment.EquipmentViewAttributeMapper;
import cn.edu.sgu.www.mhxysy.mapper.equipment.ForgeTypeMapper;
import cn.edu.sgu.www.mhxysy.service.equipment.EquipmentViewAttributeService;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.vo.equipment.EquipmentViewAttributeVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class EquipmentViewAttributeServiceImpl implements EquipmentViewAttributeService {

    private final ForgeTypeMapper forgeTypeMapper;
    private final EquipmentTypeMapper equipmentTypeMapper;
    private final EquipmentCategoryMapper equipmentCategoryMapper;
    private final EquipmentViewAttributeMapper equipmentViewAttributeMapper;

    @Autowired
    public EquipmentViewAttributeServiceImpl(
            ForgeTypeMapper forgeTypeMapper,
            EquipmentTypeMapper equipmentTypeMapper,
            EquipmentCategoryMapper equipmentCategoryMapper,
            EquipmentViewAttributeMapper equipmentViewAttributeMapper) {
        this.forgeTypeMapper = forgeTypeMapper;
        this.equipmentTypeMapper = equipmentTypeMapper;
        this.equipmentCategoryMapper = equipmentCategoryMapper;
        this.equipmentViewAttributeMapper = equipmentViewAttributeMapper;
    }

    @Override
    public void insert(EquipmentViewAttributeDTO equipmentViewAttributeDTO) {
        EquipmentViewAttribute equipmentViewAttribute = toEntity(equipmentViewAttributeDTO);

        equipmentViewAttribute.setId(null);

        equipmentViewAttributeMapper.insert(equipmentViewAttribute);
    }

    @Override
    public void updateById(EquipmentViewAttributeDTO equipmentViewAttributeDTO) {
        ObjectUtils.requireNonNull(equipmentViewAttributeDTO.getId(), CustomMessages.ID_NOT_NULL);

        EquipmentViewAttribute equipmentViewAttribute = toEntity(equipmentViewAttributeDTO);

        equipmentViewAttributeMapper.updateById(equipmentViewAttribute);
    }

    @Override
    public List<Property> selectProperty(Integer type) {
        return equipmentViewAttributeMapper.selectProperty(type);
    }

    @Override
    public List<Map<String, Object>> selectAttributes(Integer equipmentId) {
        // 防止后面的代码NPE
        if(equipmentId == null) {
            return new ArrayList<>();
        }

        EquipmentCategory equipmentCategory = equipmentCategoryMapper.selectById(equipmentId);

        // 查询顶级装备类型
        equipmentId = equipmentTypeMapper.selectTopType(equipmentCategory.getId());

        List<EquipmentViewAttributeVO> viewAttributes
                = equipmentViewAttributeMapper.selectViewAttributes(equipmentId, equipmentCategory.getGrade());
        List<ForgeType> forgeTypes = forgeTypeMapper.selectList(null);

        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = new LinkedHashMap<>();

        for (EquipmentViewAttributeVO viewAttribute : viewAttributes) {
            String propertyName = viewAttribute.getPropertyName();

            if (!map.containsKey("propertyName")) {
                map.put("propertyName", propertyName);
            }

            // 获取前面设置的属性类别
            Object name = map.get("propertyName");

            if (!propertyName.equals(name)) {
                list.add(map);

                map = new LinkedHashMap<>();
            } else {
                map.put("propertyName", propertyName);
            }

            for (ForgeType forgeType : forgeTypes) {
                if (viewAttribute.getForgeType().equals(forgeType.getId())) {
                    Range range = new Range();

                    range.setMin(viewAttribute.getMin());
                    range.setMax(viewAttribute.getMax());

                    map.put(forgeType.getName(), range);
                }
            }
        }

        list.add(map);

        return list;
    }

    @Override
    public Page<EquipmentViewAttribute> selectByPage(Pager<EquipmentViewAttribute> pager) {
        QueryWrapper<EquipmentViewAttribute> queryWrapper = Pager.getQueryWrapper(pager, true);
        Page<EquipmentViewAttribute> page = Pager.ofPage(pager);

        return equipmentViewAttributeMapper.selectPage(page, queryWrapper);
    }

    private EquipmentViewAttribute toEntity(EquipmentViewAttributeDTO equipmentViewAttributeDTO) {
        EquipmentViewAttribute equipmentViewAttribute = new EquipmentViewAttribute();

        equipmentViewAttribute.setId(equipmentViewAttributeDTO.getId());
        equipmentViewAttribute.setMax(equipmentViewAttributeDTO.getMax());
        equipmentViewAttribute.setMin(equipmentViewAttributeDTO.getMin());
        equipmentViewAttribute.setGrade(equipmentViewAttributeDTO.getGrade());
        equipmentViewAttribute.setForgeType(equipmentViewAttributeDTO.getForgeType());
        equipmentViewAttribute.setPropertyId(equipmentViewAttributeDTO.getPropertyId());
        equipmentViewAttribute.setEquipmentId(equipmentViewAttributeDTO.getEquipmentId());

        return equipmentViewAttribute;
    }

}